﻿Imports System.ComponentModel
Imports System.Web.UI
Imports System.Text

Namespace Controls
    ''' <summary> 
    ''' 控钮控件。 
    ''' </summary> 
    <DefaultEvent("Click"), DefaultProperty("Text"), ToolboxData("<{0}:Button runat=server></{0}:Button>")> _
    Public Class Button
        Inherits WebControl
        Implements IPostBackEventHandler
        ''' <summary> 
        ''' 单击事件绑定的对象 
        ''' </summary> 
        Protected Shared ReadOnly EventClick As New Object()

        Shared Sub New()
            EventClick = New Object()
        End Sub

        ''' <summary> 
        ''' 构造函数 
        ''' </summary> 
        Public Sub New()
            Me.ButtontypeMode = ButtonType.WithImage
        End Sub

        ''' <summary> 
        ''' 定义事件处理器 
        ''' </summary> 
        Public Custom Event Click As EventHandler
            AddHandler(ByVal value As EventHandler)
                Events.[AddHandler](EventClick, value)
            End AddHandler
            RemoveHandler(ByVal value As EventHandler)
                Events.[RemoveHandler](EventClick, value)
            End RemoveHandler
            RaiseEvent(ByVal sender As Object, ByVal e As System.EventArgs)

            End RaiseEvent
        End Event

        ''' <summary> 
        ''' 跨页面提交链接 
        ''' </summary> 
        Public Overridable Property PostBackUrl() As String
            Get
                Dim text1 As String = DirectCast(Me.ViewState("PostBackUrl"), String)
                If text1 IsNot Nothing Then
                    Return text1
                End If
                Return String.Empty
            End Get
            Set(ByVal value As String)
                Me.ViewState("PostBackUrl") = value
            End Set
        End Property

        ''' <summary> 
        ''' 定义Onclick 
        ''' </summary> 
        ''' <param name="e"></param> 
        Protected Overridable Sub OnClick(ByVal e As EventArgs)
            Dim clickHandler As EventHandler = DirectCast(Events(EventClick), EventHandler)
            RaiseEvent Click(Me, e)
        End Sub

        ''' <summary> 
        ''' 引发PostBack事件 
        ''' </summary> 
        ''' <param name="eventArgument"></param> 
        Public Sub MeRaisePostBackEvent(ByVal eventArgument As String)
            OnClick(New EventArgs())
        End Sub

        ''' <summary> 
        ''' 引发PostBack事件(实现IPostBackEventHandler接口) 
        ''' </summary> 
        ''' <param name="eventArgument"></param> 
        Private Sub RaisePostBackEvent(ByVal eventArgument As String) Implements IPostBackEventHandler.RaisePostBackEvent
            MeRaisePostBackEvent(eventArgument)
        End Sub

        ''' <summary> 
        ''' 重写OnPreRender 
        ''' </summary> 
        ''' <param name="e"></param> 
        Protected Overloads Overrides Sub OnPreRender(ByVal e As EventArgs)
            MyBase.OnPreRender(e)
        End Sub

        ''' <summary> 
        ''' 加载ViewState 
        ''' </summary> 
        ''' <param name="savedState"></param> 
        Protected Overloads Overrides Sub LoadViewState(ByVal savedState As Object)
            If savedState IsNot Nothing Then
                MyBase.LoadViewState(savedState)
                Dim text1 As String = DirectCast(Me.ViewState("Text"), String)
                If text1 IsNot Nothing Then
                    Me.Text = text1
                End If
            End If
        End Sub

        ''' <summary> 
        ''' 添加子对象 
        ''' </summary> 
        ''' <param name="obj"></param> 
        Protected Overloads Overrides Sub AddParsedSubObject(ByVal obj As Object)
            If Me.HasControls() Then
                MyBase.AddParsedSubObject(obj)
            ElseIf TypeOf obj Is LiteralControl Then
                Me.Text = DirectCast(obj, LiteralControl).Text
            Else
                Dim text1 As String = Me.Text
                If text1.Length <> 0 Then
                    Me.Text = String.Empty
                    MyBase.AddParsedSubObject(New LiteralControl(text1))
                End If
                MyBase.AddParsedSubObject(obj)
            End If
        End Sub

        ''' <summary> 
        ''' 输出html,在浏览器中显示控件 
        ''' </summary> 
        ''' <param name="output"></param> 
        Protected Overloads Overrides Sub Render(ByVal output As HtmlTextWriter)
            If Me.HintInfo <> "" Then
                output.WriteBeginTag("span id=""" + Me.ClientID + """ onmouseover=""showhintinfo(this," + Me.HintLeftOffSet + "," + Me.HintTopOffSet + ",'" + Me.HintTitle + "','" + Me.HintInfo + "','" + Me.HintHeight + "','" + Me.HintShowType + "');"" onmouseout=""hidehintinfo();"">")
            End If

            If Me.AutoPostBack Then
                Dim sb As StringBuilder = New System.Text.StringBuilder()
                sb.Append("if (typeof(Page_ClientValidate) == 'function') { if (Page_ClientValidate() == false) { return false; }}")
                '保证验证函数的执行 
                'sb.Append("javascript:{if (typeof(Page_ClientValidate)!='function'|| Page_ClientValidate())__doPostBack('"+this.UniqueID+"','')}"); 
                'sb.Append("if(window.confirm('are you sure?')==false) return false;"); //自定义客户端脚本 
                sb.Append("this.disabled=true;")

                If ShowPostDiv Then
                    sb.Append("document.getElementById('success').style.display = 'block';HideOverSels('success');")
                End If

                If ValidateForm Then
                    sb.Append("if(validate(this.form)){")
#If NET1 Then
                sb.Append(Page.GetPostBackEventReference(Me, "") + ";}") 
                '用__doPostBack来提交，保证按钮的服务器端click事件执行 
#Else
                    '用__doPostBack来提交，保证按钮的服务器端click事件执行 
#End If
                    sb.Append(Page.ClientScript.GetPostBackEventReference(Me, "") + ";}")
                Else
                    ' disable所有自身 
#If NET1 Then
                sb.Append(Page.GetPostBackEventReference(Me, "") + ";") 
                '用__doPostBack来提交，保证按钮的服务器端click事件执行 
#Else
                    '用__doPostBack来提交，保证按钮的服务器端click事件执行 
#End If
                    sb.Append(Page.ClientScript.GetPostBackEventReference(Me, "") + ";")
                End If

                If ScriptContent <> "" Then
                    sb.Append(ScriptContent)
                End If


                If Me.ButtontypeMode = ButtonType.Normal Then
                    output.Write("<span><button type=""button"" class=""ManagerButton"" id=""" + Me.UniqueID + """ onclick=""" + sb.ToString() + """>" + Me.Text + "</button></span>")
                End If

                If Me.ButtontypeMode = ButtonType.WithImage Then
                    output.Write("<span><button type=""button"" class=""ManagerButton"" id=""" + Me.UniqueID + """ onclick=""" + sb.ToString() + """><img src=""" + Me.ButtonImgUrl + """/>" + Me.Text + "</button></span>")

                End If
            Else
                If Me.ButtontypeMode = ButtonType.Normal Then
                    output.Write("<span><button type=""button"" class=""ManagerButton"" id=""" + Me.UniqueID + """ onclick=""" + ScriptContent + """>" + Me.Text + "</button></span>")
                End If

                If Me.ButtontypeMode = ButtonType.WithImage Then
                    output.Write("<span><button type=""button"" class=""ManagerButton"" id=""" + Me.UniqueID + """ onclick=""" + ScriptContent + """><img src=""" + Me.ButtonImgUrl + """/>" + Me.Text + "</button></span>")
                End If
            End If


            If Me.HintInfo <> "" Then
                output.WriteEndTag("span")
            End If
        End Sub

        Private _autoPostBack As Boolean = True

        ''' <summary> 
        ''' AutoPostBack属性 
        ''' </summary> 
        Public Property AutoPostBack() As Boolean
            Get
                Return Me._autoPostBack
            End Get
            Set(ByVal value As Boolean)
                Me._autoPostBack = value
            End Set
        End Property

        Private _showPostDiv As Boolean = True

        ''' <summary> 
        ''' 是否显示"正在提交"信息 
        ''' </summary> 
        Public Property ShowPostDiv() As Boolean
            Get
                Return Me._showPostDiv
            End Get
            Set(ByVal value As Boolean)
                Me._showPostDiv = value
            End Set
        End Property

#Region "定义是否调用js函数validate(this.form);进行数据校验"

        Private _validateForm As Boolean = False

        ''' <summary> 
        ''' 定义是否调用js函数validate(this.form);进行数据校验 
        ''' </summary> 
        Public Property ValidateForm() As Boolean
            Get
                Return Me._validateForm
            End Get
            Set(ByVal value As Boolean)
                Me._validateForm = value
            End Set
        End Property
#End Region

#Region "properytyButtontypeMode 按钮样式"

        ''' <summary> 
        ''' 按钮枚举样式(Normal:普通, WithImage:带图) 
        ''' </summary> 
        Public Enum ButtonType
            Normal
            '普通 
            WithImage
            '带图 
        End Enum

        ''' <summary> 
        ''' 按钮样式 
        ''' </summary> 
        Public Property ButtontypeMode() As ButtonType
            Get
                Dim obj As Object = ViewState("ButtontypeMode")
                Return IIf(obj Is Nothing, ButtonType.WithImage, DirectCast(obj, ButtonType))
            End Get
            Set(ByVal value As ButtonType)
                ViewState("ButtontypeMode") = value
            End Set
        End Property
#End Region

#Region "Property Text 按钮文字"

        ''' <summary> 
        ''' 按钮文字 
        ''' </summary> 
        <Bindable(True), Category("Appearance"), DefaultValue(" 提 交 ")> _
        Public Property Text() As String
            Get
                Dim obj As Object = ViewState("ButtonText")
                Return IIf(obj Is Nothing, " 提 交 ", DirectCast(obj, String))
            End Get
            Set(ByVal value As String)
                ViewState("ButtonText") = value
            End Set
        End Property
#End Region

#Region "Property ButtonImgUrl 图版按钮链接"

        ''' <summary> 
        ''' 图片按钮链接 
        ''' </summary> 
        <Description("图版按钮链接"), DefaultValue("../images/submit.gif")> _
        Public Property ButtonImgUrl() As String
            Get
                Dim obj As Object = ViewState("ButtonImgUrl")
                Return IIf(obj Is Nothing, "../images/submit.gif", DirectCast(obj, String))
            End Get
            Set(ByVal value As String)
                ViewState("ButtonImgUrl") = value
            End Set
        End Property

#End Region

#Region "Property XpBGImgFilePath XP背景图片路径"

        ''' <summary> 
        ''' XP背景图片路径 
        ''' </summary> 
        <Description("图版按钮链接"), DefaultValue("../images/")> _
        Public Property XpBGImgFilePath() As String
            Get
                Dim obj As Object = ViewState("XpBGImgFilePath")
                Return IIf(obj Is Nothing, "../images/", DirectCast(obj, String))
            End Get
            Set(ByVal value As String)
                ViewState("XpBGImgFilePath") = value
            End Set
        End Property

#End Region

#Region "Property ScriptContent 要加载的客户端脚本内容"

        ''' <summary> 
        ''' 要加载的客户端脚本内容 
        ''' </summary> 
        <Description("图版按钮链接"), DefaultValue("../images/")> _
        Public Property ScriptContent() As String
            Get
                Dim obj As Object = ViewState("ScriptContent")
                Return IIf(obj Is Nothing, "", DirectCast(obj, String))
            End Get
            Set(ByVal value As String)
                ViewState("ScriptContent") = value
            End Set
        End Property

#End Region
    End Class
End Namespace